package cn.s1995.sloth.app.controller;

import cn.hutool.core.collection.CollUtil;
import cn.s1995.sloth.admin.api.entity.SysPublicParam;
import cn.s1995.sloth.bizs.entity.ServiceProtocolConfig;
import cn.s1995.sloth.bizs.oo.req.VerifyREQ;
import cn.s1995.sloth.bizs.oo.req.WorldREQ;
import cn.s1995.sloth.bizs.service.ServiceProtocolConfigService;
import cn.s1995.sloth.bizs.service.common.CacheService;
import cn.s1995.sloth.bizs.service.common.CommonService;
import cn.s1995.sloth.common.core.util.R;
import cn.s1995.sloth.common.security.annotation.Inner;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * CommonController
 *
 * @author Lex
 * @description 公共接口
 * @date 2023/12/23 17:14
 */
@RestController
@RequestMapping("/m/common")
@RequiredArgsConstructor
@Tag(description = "CommonController", name = "APP-公共接口")
@Inner(value = false)
public class ApiCommonController {

    private final CommonService commonService;
    private final ServiceProtocolConfigService serviceProtocolConfigService;
    private final CacheService cacheService;

    /**
     * 获取服务协议列表
     *
     * @param typeList
     * @return
     */
    @ApiOperationSupport(order = 1)
    @Operation(summary = "获取服务协议列表", description = "获取列表")
    @GetMapping("/service-protocol-config/list")
    public R<List<ServiceProtocolConfig>> getListServiceProtocolConfig(@ParameterObject @RequestParam List<Integer> typeList) {
        LambdaQueryWrapper<ServiceProtocolConfig> wrapper = Wrappers.lambdaQuery();
        wrapper.in(CollUtil.isNotEmpty(typeList), ServiceProtocolConfig::getType, typeList);
        return R.ok(serviceProtocolConfigService.list(wrapper));
    }

    /**
     * 获取服务协议对象
     *
     * @param type 类型
     * @return R
     */
    @ApiOperationSupport(order = 20)
    @Operation(summary = "获取服务协议对象", description = "通过类型查询")
    @Parameter(name = "type", description = "类型：1.用户协议 2.隐私政策 3.服务协议 4.退换货政策 5.退款政策 6.帮助中心(FAQ) 7-运输与交付", in = ParameterIn.QUERY)
    @GetMapping("/service-protocol-config")
    public R<ServiceProtocolConfig> getServiceProtocolConfig(@RequestParam Integer type) {
        return R.ok(
                serviceProtocolConfigService.getOne(
                        Wrappers.lambdaQuery(ServiceProtocolConfig.class)
                                .eq(ServiceProtocolConfig::getType, type),
                        Boolean.FALSE
                )
        );
    }

    @ApiOperationSupport(order = 30)
    @Operation(summary = "验证码-发送", description = "根据类型/场景按需发送验证码")
    @PostMapping("/verify/send")
    public R verifySendCode(@RequestBody VerifyREQ req) {
        return commonService.verifySendCode(req);
    }

    @ApiOperationSupport(order = 31)
    @Operation(summary = "验证码-验证", description = "根据根据类型/场景按需验证短信验证码,返回code=0成功，否则返回对应错误码以及错误提示")
    @PostMapping("/verify/check")
    public R verifyCheck(@RequestBody VerifyREQ req) {
        return commonService.verifyCheck(req);
    }

    @ApiOperationSupport(order = 40)
    @Operation(summary = "获取世界地理数据", description = "根据场景按需获取世界地理数据")
    @GetMapping("/world-geo")
    public R world(@ParameterObject WorldREQ req) {
        return R.ok(commonService.getWorldData(req));
    }

    @ApiOperationSupport(order = 50)
    @Operation(summary = "获取公共参数", description = "")
    @Parameter(
            name = "keys",
            description = "keys可单个可多个(\",\"分隔)可不传（不传则返回全部配置信息） <br/>" +
                    "联系我们   ->  CONTACT_US <br/>" +
                    "是否启用短信通道   ->  IS_ENABLE_SMS <br/>" +
                    "短信发送次数限制   ->  SMS_SEND_NUM_LIMIT <br/>" +
                    "获取订单自动取消时间(单位：分钟)   ->  ORDER_AUTO_CANCEL_TIME <br/>" +
                    "获取订单自动收货时间(单位：天)   ->  ORDER_AUTO_CONFIRM_TIME <br/>"
            ,
            in = ParameterIn.QUERY)
    @GetMapping("/public-params")
    public R<Map<String, SysPublicParam>> getSettings(@RequestParam(required = false) String ...keys) {
        return R.ok(cacheService.getSysParams(keys));
    }

}
